from django.shortcuts import render
from django.shortcuts import HttpResponse,render,redirect
from app01 import models
# Create your views here.


def judge_right(name,pwd):
    if models.User.objects.filter(username=name,password=pwd):
        return True
    else:
        return False


def index(request):
    rets = models.User.objects.all()#ret是一个对象列表,获取的是表里面所有的数据
    # for i in rets:
    #     print(i,i.username,i.password)
    # print(rets,type(rets))

    # ret = models.User.objects.get(username='perururu')#只能匹配一个
    # print("一个字段是" +ret.password)

    return render(request,'main.html')

def login(request):
    """
    判断是否通过并跳转
    :param request:
    :return:
    """
    #print(request,type(request))
    if request.method == "GET":
        #print(type(request))
        return render(request,'login.html')

    elif request.method == "POST":
        print(request.POST,type(request.POST))
        print("request中的user是" + request.POST['user'])
        name = request.POST['user']
        pwd = request.POST['pwd']
        if judge_right(name,pwd):
            #return render(request,'success.html')
            return redirect('/index/')
        else:
            print("登陆失败")
            return render(request,'login.html')

def login2(request):
    """

    :param request:
    :return: render login2.html
    """
    return render(request,'login2.html')
